<template>
  <div>
    <el-table :data="userlist" border style="width: 100%">
      <el-table-column prop="uid" label="用户编号" width="180">
      </el-table-column>
      <el-table-column prop="username" label="用户名称" width="180">
      </el-table-column>
      <el-table-column prop="rolename" label="所属角色" width="180">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            <span v-if="item.row.status == 1">启用</span>
            <span v-else>禁用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              circle
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(item.row.uid)"
            ></el-button>
            <el-button
              circle
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(item.row.uid)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页视图 -->
    <el-pagination
      :page-size="3"
      background
      layout="prev, pager, next"
      :total="count"
      @current-change='changePage'
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { userDelete } from "../../request/api";
export default {
  data() {
    return {};
  },
  mounted() {
    //组件一加载就触发管理员列表调用的行动
    this.getUser();
    //调用总条数
    this.getCount();
  },
  computed: {
    ...mapGetters({
      userlist: "user/getUser",
      count: "user/getCount",
    }),
  },
  methods: {
    ...mapActions({
      getUser: "user/getUserAction",
      getCount: "user/getCountAction",
      changePageAction:"user/changePageAction",
    }),
    //封装一个切换页码事件
    changePage(n){
      //console.log(n,'当前页');
      this.changePageAction(n)
    },
    //封装一个编辑事件
    edit(uid) {
      this.$emit("edit", uid);
    },
    //封装一个删除事件
    del(uid) {
      this.$confirm("你确定删除这条数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          userDelete({ uid }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.getUser();
              //调用总条数
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
</style>